﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
// COM 组件接口
using System.Runtime.InteropServices;

namespace CKVisionCtrlDemo_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void LOAD_FILE_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openfileDlg = new OpenFileDialog();

                openfileDlg.Filter = "vscf files (*.vscf)|*.vscf";
                openfileDlg.FilterIndex = 1;
                openfileDlg.RestoreDirectory = true;

                if (openfileDlg.ShowDialog() == DialogResult.OK)
                {
                    string strFilePathName = openfileDlg.FileName.ToString();
                    axCKVisionCtrl1.LoadConfigure(strFilePathName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }

        }

        private void SAVE_FILE_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog savefileDlg = new SaveFileDialog();

                savefileDlg.Filter = "ckcf files (*.vscf)|*.vscf";
                savefileDlg.FilterIndex = 1;
                savefileDlg.RestoreDirectory = true;

                if (savefileDlg.ShowDialog() == DialogResult.OK)
                {
                    string FilePathName = savefileDlg.FileName.ToString();
                    bool isSave = axCKVisionCtrl1.SaveConfigure(FilePathName);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not save file to the disk. Original error: " + ex.Message);
            }

        }

        private void EDIT_PROC_Click(object sender, EventArgs e)
        {
            try
            {
                axCKVisionCtrl1.ShowEditor( "编辑流程", 0 );
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not edit proc. Original error: " + ex.Message);
            }
        }

        private void EXECUTE_Click(object sender, EventArgs e)
        {
            try
            {
                axCKVisionCtrl1.Execute( 0 );
                axCKVisionCtrl1.Redraw();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not extcute proc. Original error: " + ex.Message);
            }
        }

        private void GET_DATA_Click(object sender, EventArgs e)
        {
            string strToolName = textBox1.Text;
            int idTool = axCKVisionCtrl1.GetTool(strToolName);   

            int DataId = Convert.ToInt32(textBox2.Text);
            int index = Convert.ToInt32(txtIndex.Text);

            double value = 0.0;
            object objValue = new VariantWrapper(value);

            if (axCKVisionCtrl1.GetValue(idTool, DataId, index, ref objValue) == true)
            {
                textBox3.Text = objValue.ToString().Substring(0,6);
            }
            else
            {
                MessageBox.Show("获取数据失败! ");
            }
 
        }

        private void ZOOM_IN_Click(object sender, EventArgs e)
        {
            axCKVisionCtrl1.ZoomView(0);
            axCKVisionCtrl1.Redraw();
        }

        private void ZOOM_OUT_Click(object sender, EventArgs e)
        {
            axCKVisionCtrl1.ZoomView(1);
            axCKVisionCtrl1.Redraw();
        }

        private void ZOOM_FIT_Click(object sender, EventArgs e)
        {
            axCKVisionCtrl1.ZoomView(2);
            axCKVisionCtrl1.Redraw();
        }

        private void ZOOM_1X1_Click(object sender, EventArgs e)
        {
            axCKVisionCtrl1.ZoomView(3);
            axCKVisionCtrl1.Redraw();
        }

        private void TOOL_Property_Click(object sender, EventArgs e)
        {
            string strToolName = textBox1.Text;
            int idTool = axCKVisionCtrl1.GetTool(strToolName);
            axCKVisionCtrl1.ShowProperty(idTool );
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            axCKVisionCtrl1.UnloadConfigure();
        }


    }
}
